package com.zjw.study.thread03_mutex;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 模拟线程并发情况
 * 
 * 结论：同步锁依然有效
 * @author Administrator
 *
 */
public class ThreadMutexExecutor {

	public static void main(String[] args) {
		ThreadMutexExecutor te = new ThreadMutexExecutor();
		te.executor02();
	}
	
	void executor01() {
		ExecutorService executor = Executors.newCachedThreadPool();//创建新城池
		for(int i = 0; i < 50; i++) {
			executor.execute(new Runnable() {
				
				@Override
				public void run() {
					sleep();
					out("zhangsanhelisizaihebianwanshua");
				}
			});
		}
	}
	
	void executor02() {
		executor01();
		System.out.println("2222");
		executor01();
	}
	
	void out(String str) {
		int length = str.length();
		synchronized (str) {
			
			for(int i = 0; i < length; i++) {
				System.out.print(str.charAt(i));
			}
			System.out.println();
		}
	}
	
	private void sleep() {
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
